package com.zr.service.driverclient.impl;

import com.zr.dao.order.OrderFormDao;
import com.zr.service.driverclient.UploadService;
import com.zr.utils.RedisUtil;
import com.zr.vo.order.OrderForm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

@Service
public class UploadServiceImpl implements UploadService {
    @Resource
    private OrderFormDao orderFormDao;

    @Resource
    private RedisUtil redisUtil;

    @Value("${img.path}")
    private String imgPath;
    /**
     * 保存订单的回执单图片
     * @param file
     * @param id
     * @param orderId
     * @return
     * @throws IOException
     */
    @Override
    public Object upload(MultipartFile file, Integer id,String orderId) throws IOException {
        if(file.isEmpty()) return  0;
        String imgUrl =  orderId + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(imgPath + imgUrl));
       //订单到中存入订单图片信息
        OrderForm orderForm=new OrderForm();
        orderForm.setId(id);
        orderForm.setFinishImg(imgUrl);
        return orderFormDao.updateByPrimaryKeySelective(orderForm);

    }

    /**
     * 保存异常的图片
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public int saveAbnormal(MultipartFile file) throws IOException {
        if(file.isEmpty()) return  0;
        String aid= (String) redisUtil.get("aid");
        String imgUrl = aid + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(imgPath + imgUrl));
        return 1;
    }

    /**
     * 获取订单图片
     * @param name
     * @return
     */
    @Override
    public String getImg(String name) {

        return getImgs(name,1);
    }

    /**
     * 获取异常订单图片
     * @param aid
     * @return
     */
    @Override
    public String getAbnImg(String aid) {
        return getImgs(aid,2);
    }

    /**
     * 获取图片的方法
     * @param name
     * @param number
     * @return
     */
    private String getImgs(String name,int number){

        if(!StringUtils.hasLength(name)) return "";

        switch (number){
            case 1:
                File file = new File(imgPath + name );
                if(file.exists()){
                    return getImgBase(imgPath + name);
                }
                break;

            case 2:
                File file2 = new File(imgPath + name + ".png" );
                if(file2.exists()){
                    return getImgBase(imgPath + name + ".png" );
                }
                break;
            default:break;
        }
        return "";
    }


    /**
     * 将图片转换成Base64编码
     * @param imgFile 待处理图片地址
     * @return
     */
    public static String getImgBase(String imgFile) {
        // 将图片文件转化为二进制流
        InputStream inputStream = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 图片头
        return Base64.getEncoder().encodeToString(data);
    }


}
